package com.milo.java8.function;

import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author milogenius
 * @date 2020-05-29 13:25
 */
public class Filters {

    /**
     * 根据类型筛选瓜类
     * @param melons 瓜类
     * @param type 类型
     * @return
     */
    public static List<Melon> filterByType(List<Melon> melons, String type) {

        List<Melon> result = new ArrayList<>();
        for (Melon melon: melons) {
            if (melon != null && type.equalsIgnoreCase(melon.getType())) {
                result.add(melon);
            }
        }
        return result;
    }

    /**
     * 按照重量过滤瓜类
     * @param melons
     * @param weight
     * @return
     */
    public static List<Melon> filterByWeight(List<Melon> melons, int weight) {

        List<Melon> result = new ArrayList<>();
        for (Melon melon: melons) {
            if (melon != null && melon.getWeight() == weight) {
                result.add(melon);
            }
        }
        return result;
    }

    /**
     * 按照类型和重量来筛选瓜类
     * @param melons
     * @param type
     * @param weight
     * @return
     */
    public static List<Melon> filterByTypeAndWeight(List<Melon> melons, String type, int weight) {

        List<Melon> result = new ArrayList<>();
        for (Melon melon: melons) {
            if (melon != null && type.equalsIgnoreCase(melon.getType()) && melon.getWeight() == weight) {
                result.add(melon);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        ArrayList<Melon> melons = new ArrayList<>();
        melons.add(new Melon("羊角蜜", 1, "泰国"));
        melons.add(new Melon("西瓜", 2, "三亚"));
        melons.add(new Melon("黄河蜜", 3, "兰州"));
        List<Melon> melonType = Filters.filterByType(melons, "黄河蜜");
        melonType.forEach(melon->{
            System.out.println("瓜类型:"+melon.getType());
        });

        List<Melon> melonWeight = Filters.filterByWeight( melons,3);
        melonWeight.forEach(melon->{
            System.out.println("瓜重量:"+melon.getWeight());
        });

        List<Melon> melons1 = Filters.filterByTypeAndWeight(melons, "黄河蜜", 3);
        melons1.forEach(melon->{
            System.out.println("瓜类型:"+melon.getType()+"  瓜重量:"+melon.getWeight());
        });
        
    }
}
